package org.jsola.hr.service.impl;

import org.jsola.common.DateKit;
import org.jsola.core.Page;
import org.jsola.hr.common.DateUtils;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISalaryChangesDAO;
import org.jsola.hr.dto.SalaryChangesAddDTO;
import org.jsola.hr.dto.SalaryChangesUpdateDTO;
import org.jsola.hr.entity.SalaryChangesDO;
import org.jsola.hr.query.SalaryChangesQuery;
import org.jsola.hr.service.ISalaryChangesService;
import org.jsola.hr.vo.SalaryChangesListVO;
import org.jsola.hr.vo.SalaryChangesVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrSalaryChangesServiceImpl")
public class SalaryChangesServiceImpl implements ISalaryChangesService {

    @Autowired
    private ISalaryChangesDAO salaryChangesDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SalaryChangesVO save(SalaryChangesAddDTO salaryChangesAddDTO, TokenUser tokenUser) {
        // 转DO
        SalaryChangesDO salaryChangesDO = salaryChangesAddDTO.to(SalaryChangesDO.class);

        //生效时间是否小于等于当前时间
        if(DateUtils.timeCompare(DateKit.getDateStr(salaryChangesDO.getEffectiveDate()), DateKit.getDateStr(new Date())) <= 0){
            //修改当前记录为已生效
            salaryChangesDO.setEffectiveStatus(true);
        }

        // 保存
        salaryChangesDO = save(salaryChangesDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("添加员工调薪记录", "SalaryChanges", salaryChangesDO.getId(), tokenUser);

        return salaryChangesDO.to(SalaryChangesVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SalaryChangesUpdateDTO salaryChangesUpdateDTO, TokenUser tokenUser) {
        //转DO
        SalaryChangesDO salaryChangesDO = salaryChangesUpdateDTO.to(SalaryChangesDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(salaryChangesDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... salaryChangesIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) salaryChangesIds);
    }


    @Override
    public SalaryChangesVO selectById(String salaryChangesId, String siteId) {
        SalaryChangesDO salaryChangesDO = selectDOById(salaryChangesId, siteId);
        if (salaryChangesDO == null) {
            return null;
        }
        return salaryChangesDO.to(SalaryChangesVO.class);
    }

    @Override
    public List<SalaryChangesListVO> select(SalaryChangesQuery salaryChangesQuery, String siteId) {
        List<SalaryChangesDO> salaryChangesDOList = selectDO(salaryChangesQuery, siteId);
        if(CollectionUtils.isEmpty(salaryChangesDOList)) {
            return salaryChangesDOList == null ? null : new ArrayList<>();
        }
        return salaryChangesDOList.stream()
                .map(salaryChangesDO -> salaryChangesDO.to(SalaryChangesListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SalaryChangesQuery salaryChangesQuery, String siteId) {
        Example example = buildExample(salaryChangesQuery,siteId);
        return salaryChangesDAO.selectCountByExample(example);
    }

    @Override
    public Page<SalaryChangesListVO> selectPage(SalaryChangesQuery salaryChangesQuery, String siteId) {
        Example example = buildExample(salaryChangesQuery,siteId);
        Page<SalaryChangesDO> page = salaryChangesDAO.selectPageByExample(example,
                salaryChangesQuery.getPageNo(),
                salaryChangesQuery.getPageSize());

        return page.to(SalaryChangesListVO.class);
    }

    @Override
    public SalaryChangesDO selectDOById(String salaryChangesId, String siteId) {
        return listById(salaryChangesId, siteId);
    }

    @Override
    public List<SalaryChangesDO> selectDO(SalaryChangesQuery salaryChangesQuery, String siteId) {
        Example example = buildExample(salaryChangesQuery,siteId);
        return salaryChangesDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param salaryChangesQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SalaryChangesQuery salaryChangesQuery, String siteId) {
        Example example = new Example(SalaryChangesDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (salaryChangesQuery.getEmpId() != null) {
            example.and().andLike("empId", "%" + salaryChangesQuery.getEmpId() + "%");
        }
        if (salaryChangesQuery.getEffectiveStatus() != null) {
            example.and().andEqualTo("effectiveStatus", salaryChangesQuery.getEffectiveStatus());
        }
        if (salaryChangesQuery.getEffectiveDateStart() != null &&
                salaryChangesQuery.getEffectiveDateEnd() != null) {
            example.and().andBetween("effectiveDate", salaryChangesQuery.getEffectiveDateStart(),
                    salaryChangesQuery.getEffectiveDateEnd());
        }
        if (salaryChangesQuery.getChangeReason() != null) {
            example.and().andLike("changeReason", "%" + salaryChangesQuery.getChangeReason() + "%");
        }
        if (salaryChangesQuery.getSalaryNew() != null) {
            example.and().andEqualTo("salaryNew", salaryChangesQuery.getSalaryNew());
        }
        if (salaryChangesQuery.getTrafficSubsidyNew() != null) {
            example.and().andEqualTo("trafficSubsidyNew", salaryChangesQuery.getTrafficSubsidyNew());
        }
        if (salaryChangesQuery.getMealSubsidyNew() != null) {
            example.and().andEqualTo("mealSubsidyNew", salaryChangesQuery.getMealSubsidyNew());
        }
        if (salaryChangesQuery.getSalaryPerformanceNew() != null) {
            example.and().andEqualTo("salaryPerformanceNew", salaryChangesQuery.getSalaryPerformanceNew());
        }
        if (salaryChangesQuery.getSalaryTotalNew() != null) {
            example.and().andEqualTo("salaryTotalNew", salaryChangesQuery.getSalaryTotalNew());
        }
        if (salaryChangesQuery.getRemark() != null) {
            example.and().andLike("remark", "%" + salaryChangesQuery.getRemark() + "%");
        }
        if (salaryChangesQuery.getChangeType() != null) {
            example.and().andEqualTo("changeType", salaryChangesQuery.getChangeType());
        }
        if (salaryChangesQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", salaryChangesQuery.getCompanyId());
        }
        if (salaryChangesQuery.getGmtCreateStart() != null &&
                salaryChangesQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", salaryChangesQuery.getGmtCreateStart(),
                    salaryChangesQuery.getGmtCreateEnd());
        }
        if (salaryChangesQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + salaryChangesQuery.getCreateUserId() + "%");
        }
        if (salaryChangesQuery.getGmtModifiedStart() != null &&
                salaryChangesQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", salaryChangesQuery.getGmtModifiedStart(),
                    salaryChangesQuery.getGmtModifiedEnd());
        }
        if (salaryChangesQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + salaryChangesQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,salaryChangesQuery.getOrders());
        return example;
    }

    @Override
    public SalaryChangesDO getSalaryBasicByEmpId(String empId, String siteId){
        return salaryChangesDAO.getSalaryBasicByEmpId(empId, siteId);
    }
}




